package com.ht.web360.PojServer.service;

import com.ht.web360.base.dto.PojInfo.dto.PojBasicDto;
import com.ht.web360.base.dto.PojInfo.dto.PojInfoDto;
import com.ht.web360.base.dto.RPage;
import com.ht.web360.base.dto.Res;
import com.ht.web360.base.dto.UserInfo.dto.UserInfoDto;
import com.ht.web360.base.feignApi.UserClient;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PojBasicService {

    private final PojInfoService pojInfoService;
    private final UserClient userClient;

    public PojBasicService(PojInfoService pojInfoService, UserClient userClient) {
        this.pojInfoService = pojInfoService;
        this.userClient = userClient;
    }

    public RPage<PojBasicDto> pageBasicData(long current, long size) {
        RPage<PojInfoDto> pojInfoPage = pojInfoService.pageInfo(current, size);
        Map<String, UserInfoDto> userInfoCache = new HashMap<>();
        return RPage.getInstance(pojInfoPage, item -> {
            UserInfoDto dto = userInfoCache.get(item.getCreatorId());
            PojBasicDto res = null;
            if (dto == null) {
                Res<UserInfoDto> userById = userClient.getUserById(Long.parseLong(item.getId()));
                if (userById.isStatus()) {
                    dto = userById.getData();
                    userInfoCache.put(item.getId(), dto);
                }
                res = PojBasicDto.getInstance(item, null, null);
            }
            return res;
        });
    }
}
